 package com.reremouse.rerebbs.dao.impl;

 import com.reremouse.rerebbs.dao.IBbsThreadDAO;
 import com.reremouse.rerebbs.model.BbsThread;

 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import java.io.Serializable;
 import java.util.List;
 
 public class BbsThreadDAOImpl
         implements IBbsThreadDAO, Serializable
 {
   @PersistenceContext
   private EntityManager entityManager;
   public static final String TITLE = "title";
   public static final String GEN_BY = "genBy";
   public static final String LAST_REPLY_BY = "lastReplyBy";
   public static final String ZONE = "zone";
   public static final String READ_COUNT = "readCount";
   
   public EntityManager getEntityManager()
   {
     return this.entityManager;
   }
   
   public void setEntityManager(EntityManager entityManager)
   {
     this.entityManager = entityManager;
   }
   
   public void topThread(long t)
   {
     BbsThread bt = (BbsThread)this.entityManager.find(BbsThread.class, Long.valueOf(t));
     bt.setTop(true);
     this.entityManager.merge(bt);
   }
   
   public void unTopThread(long t)
   {
     BbsThread bt = (BbsThread)this.entityManager.find(BbsThread.class, Long.valueOf(t));
     bt.setTop(false);
     this.entityManager.merge(bt);
   }
   
   public long getTalksNumByThread(long id)
   {
     String q = "Select count(ms) from BbsTalk ms where ms.thread.id=" + id;
     long num = ((Long)this.entityManager.createQuery(q).getResultList().get(0)).longValue();
     return num;
   }
   
   public void save(BbsThread entity)
   {
     try
     {
       this.entityManager.persist(entity);
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
   
   public void delete(long id)
   {
     try
     {
       BbsThread entity = (BbsThread)this.entityManager.getReference(BbsThread.class, Long.valueOf(id));
       
       this.entityManager.remove(entity);
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
   
   public BbsThread update(BbsThread entity)
   {
     try
     {
       return (BbsThread)this.entityManager.merge(entity);
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
   
   public BbsThread findById(Long id)
   {
     try
     {
       return (BbsThread)this.entityManager.find(BbsThread.class, id);
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
   
   public List<BbsThread> findByProperty(String propertyName, Object value, int... rowStartIdxAndCount)
   {
     try
     {
       String queryString = "select model from BbsThread model where model." + propertyName + "= :propertyValue order by model.top desc,model.latestTalk.genTime desc";
       
       Query query = this.entityManager.createQuery(queryString);
       query.setParameter("propertyValue", value);
       if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0))
       {
         int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
         if (rowStartIdx > 0) {
           query.setFirstResult(rowStartIdx);
         }
         if (rowStartIdxAndCount.length > 1)
         {
           int rowCount = Math.max(0, rowStartIdxAndCount[1]);
           if (rowCount > 0) {
             query.setMaxResults(rowCount);
           }
         }
       }
       return query.getResultList();
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
   
   public List<BbsThread> findByTitle(Object title, int... rowStartIdxAndCount)
   {
     return findByProperty("title", title, rowStartIdxAndCount);
   }
   
   public List<BbsThread> findByGenBy(Object genBy, int... rowStartIdxAndCount)
   {
     return findByProperty("genBy", genBy, rowStartIdxAndCount);
   }
   
   public List<BbsThread> findByLastReplyBy(Object lastReplyBy, int... rowStartIdxAndCount)
   {
     return findByProperty("lastReplyBy", lastReplyBy, rowStartIdxAndCount);
   }
   
   public List<BbsThread> findByZone(long id)
   {
     String queryString = "select model from BbsThread model where model.zone.id=" + id + " order by model.top desc,model.latestTalk.genTime desc";
     List<BbsThread> ts = this.entityManager.createQuery(queryString).getResultList();
     return ts;
   }
   
   public List<BbsThread> findByReadCount(Object readCount, int... rowStartIdxAndCount)
   {
     return findByProperty("readCount", readCount, rowStartIdxAndCount);
   }
   
   public List<BbsThread> findAll(int... rowStartIdxAndCount)
   {
     try
     {
       String queryString = "select model from BbsThread model order by model.top desc,model.latestTalk.genTime desc";
       Query query = this.entityManager.createQuery("select model from BbsThread model order by model.top desc,model.latestTalk.genTime desc");
       if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0))
       {
         int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
         if (rowStartIdx > 0) {
           query.setFirstResult(rowStartIdx);
         }
         if (rowStartIdxAndCount.length > 1)
         {
           int rowCount = Math.max(0, rowStartIdxAndCount[1]);
           if (rowCount > 0) {
             query.setMaxResults(rowCount);
           }
         }
       }
       return query.getResultList();
     }
     catch (RuntimeException re)
     {
       throw re;
     }
   }
 }



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.BbsThreadDAOImpl

 * JD-Core Version:    0.7.0.1

 */